﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using AMing.Helper.Net40;
using AMing.Api.Client;


namespace Html5Music.Data
{
    public class DataBaseForApi<T> where T : class ,new()
    {
        public DataBaseForApi(string key)
        {
            this.ApiKey = key;
        }
        /// <summary>
        /// 文件路径
        /// </summary>
        public string ApiKey { get; set; }
        /// <summary>
        /// 数据
        /// </summary>
        private T _data = null;

        public T Data
        {
            get
            {
                if (_data == null)
                {
                    _data = new T();
                }
                return _data;
            }
            set { _data = value; }
        }

        /// <summary>
        /// 读取
        /// </summary>
        public void Load()
        {
            KeyValueHelper apiHelper = new KeyValueHelper();
            this.Data = apiHelper.Value<T>(this.ApiKey);
        }
        /// <summary>
        /// 保存
        /// </summary>
        public void Save()
        {
            string json = JsonHelper.Serialize(this.Data);
            KeyValueHelper apiHelper = new KeyValueHelper();
            apiHelper.AddOrUpdate(new AMing.Api.Model.KeyValue
            {
                Key = this.ApiKey,
                Value = json
            });
        }
    }
}
